
import PcCookie from '../../utils/cookie/cookie'
import {enums} from '../../utils/settings'
import PcLockr from "../lockr/lockrs";

const state = {
    loginName: '',
    password:'',
    rememberMe: false,
    menuList: [{
        id:'',
        name:'',
        path:'',
        price:'',
    }],
};

const getters = {
    getMenuList:(state) =>{
      return state.menuList;
    },
    getLoginName: (state)=>{
        if(!state.loginName){
            state.loginName = PcCookie.get(enums.USER.LOGIN_NAME);
        }
        return state.loginName;
    },
    getPassword:(state)=>{
        return state.password;}
};

const mutations = {
    updateUserInfo (state, loginName) {
        state.loginName = loginName;
        PcCookie.set({
            key: enums.USER.LOGIN_NAME,
            value: loginName
        });
    },
    updateUserMenu (state, menuList) {
        state.menuList = menuList;
        if(state.menuList.length>4){
            state.menuList.slice(0,4);
        }
        PcLockr.set(enums.USER.MENU_LIST, menuList);
    },
    appendUserMenu(state, menuList) {
       if(state.menuList.length>4){
          state.menuList = state.menuList.slice(1,4);
       }
       state.menuList.push(menuList)
       PcLockr.set(enums.USER.MENU_LIST, state.menuList)
    },
    deleteUserInfo (state) {
        PcCookie.delete({
            key: enums.USER.LOGIN_NAME
        });
        state.loginName = '';
        PcCookie.delete({
            key: enums.USER.REMEMBER_ME
        });
        state.rememberMe = false;
    },
    deleteMenuList (state) {
        PcLockr.delete(enums.USER.MENU_LIST);
        state.menuList = {};
    },
    addAcc(state,acc,pass){
        state.loginName = acc;
        state.password  = pass;
    }
};

const actions = {

};


export default {
    state,
    getters,
    actions,
    mutations
};
